package com.cily.utils.t_webrtc.singlr.pad;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.cily.utils.app.event.Event;
import com.cily.utils.log.L;
import com.cily.utils.t_webrtc.BaseAc;
import com.cily.utils.t_webrtc.BaseAc3;
import com.cily.utils.t_webrtc.Conf;
import com.cily.utils.t_webrtc.R;
import com.cily.utils.t_webrtc.pad.VideoAc;
import com.cily.utils.t_webrtc.singlr.Beans.MessageBean;
import com.cily.utils.t_webrtc.singlr.Beans.SRUserBean;
import com.cily.utils.t_webrtc.singlr.Services.ActionMessageService;
import com.cily.utils.t_webrtc.singlr.Services.SignalRService;

import java.util.ArrayList;
import java.util.List;

public class ConnectToActivity extends BaseAc3 {
    private LinearLayout linear_users;
   private TextView  tv_room_title;
    private ImageView iv_user_0;
    private ImageView iv_user_1;
    private ImageView iv_user_2;
    private String roomNumber;
    private String reciever = "111";
    private List<SRUserBean> userList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect_to);
        //注册

        Bundle bundle = this.getIntent().getExtras();
        String block = bundle.getString("block");
        String floor = bundle.getString("floor");
        String unit = bundle.getString("unit");
        tv_room_title = (TextView)findViewById(R.id.tv_room_title);
        tv_room_title.setText("BLOCK "+block+"  FLOOR "+floor+"  UNIT "+unit);
        linear_users = (LinearLayout)findViewById(R.id.linear_users_id);
        roomNumber = block+floor+unit;


        iv_user_0 = findImg(R.id.user_0);
        iv_user_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reciever = userList.get(0).getConnectionId();
            }
        });
        iv_user_1 = findImg(R.id.user_1);
        iv_user_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reciever = userList.get(1).getConnectionId();
            }
        });
        iv_user_2 = findImg(R.id.user_2);
        iv_user_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reciever = userList.get(2).getConnectionId();
            }
        });

        findImg(R.id.img_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        findImg(R.id.img_btn_call).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                if(reciever.isEmpty()){
                if(reciever == null || reciever.isEmpty()){
                    new AlertDialog.Builder(ConnectToActivity.this).setTitle("系統提示")//设置对话框标题
                            .setMessage("請選擇您要呼叫的住戶！")//设置显示的内容
                            .setPositiveButton("確定",new DialogInterface.OnClickListener() {//添加确定按钮
                                @Override
                                public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                    dialog.dismiss();
                                }
                            });
                } else {
                    //TODO 呼叫住户
                    startTimer();
                    play();

                    //TODO
//                    SignalRService.mConnection.send(ActionMessageService.callVedio(reciever,""));
//                    debugToast("已经呼叫"+reciever);
                    Bundle bundle = new Bundle();
//                    bundle.putString("reciever", reciever);
                    //TODO 待修改
//                    bundle.putString("reciever", roomNumber);
                    bundle.putString(Conf.EXTRA_ROOMID, reciever);
                    bundle.putBoolean(Conf.INTENT_FROM_PAD, true);
                    showToast("reciever:"+reciever);
                    toAc(VideoAc.class, bundle);

               }
            }
        });
    }

    @Override
    protected void timerFinish() {
        super.timerFinish();
        //TODO 倒计时结束


    }

    private void loadOnlineUser(String roomNumber) {
        SignalRService.mConnection.send(ActionMessageService.getOnlineUser(roomNumber));
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadOnlineUser("111");
    }

    @Override
    public void doEvent(Event e) {
        final MessageBean messageBean = (MessageBean)e.obj;// com.alibaba.fastjson.JSONObject.parseObject((String) e.obj, MessageBean.class);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(messageBean.toString());
            }
        });
        userList = new ArrayList<>();
        if(messageBean != null) {
            int code = messageBean.getCode();
            if(code == 999) {
                com.alibaba.fastjson.JSONArray array = com.alibaba.fastjson.JSONArray.parseArray(messageBean.getData());
                for (int i = 0; i < array.size(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    SRUserBean cr = (SRUserBean) JSONObject.toJavaObject(jsonObject,SRUserBean.class);
                    userList.add(cr);
                }
                final int l = userList.size();
                if(l>0){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            iv_user_0.setVisibility(View.VISIBLE);
                            reciever = userList.get(0).getConnectionId();
                            if(l>1) {
                                iv_user_1.setVisibility(View.VISIBLE);
                            }
                            if(l>2) {
                                iv_user_2.setVisibility(View.VISIBLE);
                            }
                        }
                    });
                }
            }
        }
    }

    @Override
    protected void doResponse(Object b) {

    }
}
